package interceptor;

import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AuthorityInterceptor extends AbstractInterceptor 
{

	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		 // 鍙栧緱璇锋眰鐨凙ction鍚�
        String    name = invocation.getInvocationContext().getName();

        if (name.equals("Login")) {
            // 濡傛灉鐢ㄦ埛鎯崇櫥褰曪紝鍒欎娇涔嬮�杩�
            return invocation.invoke();
        } else {
            // 鍙栧緱Session銆�
            ActionContext ac = invocation.getInvocationContext();
            @SuppressWarnings("unchecked")
			Map<String, String> session =    (Map<String,String>)ac.get(ServletActionContext.SESSION);

            if (session == null) {
                // 濡傛灉Session涓虹┖锛屽垯璁╃敤鎴风櫥闄嗐�
                return "login";
            } else {
                String username = (String)session.get("username");
                if (username == null) {
                    // Session涓嶄负绌猴紝浣哠ession涓病鏈夌敤鎴蜂俊鎭紝
                    // 鍒欒鐢ㄦ埛鐧婚檰
                    return "login";
                } else {
                    // 鐢ㄦ埛宸茬粡鐧婚檰锛屾斁琛寏
                    return invocation.invoke();
                }
            }
        }
	}

}
